//
//  DelayFooter.swift
//  DSP
//
//  Created by tucici on 2024/5/16.
//

import UIKit
import TCCKit
let updateDelay = "updateDelay"
class DelayFooter: UICollectionReusableView {
    private var leftBtn : UIButton!
    private var rightBtn : UIButton!
    private var slider : DSlider!
    var block : ((ChannelModel)->Void)?
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        getBtn("-", &leftBtn)
        getBtn("+", &rightBtn)
        func getBtn(_ img:String, _ sender:inout UIButton?){
            let btn = UIButton(type: .custom)
            btn.setImage(UIImage(named: img), for: .normal)
            btn.cornerRadius = 4.k_h
            btn.addTarget(self, action: #selector(actionForBtn), for: .touchUpInside)
            sender = btn
        }
        
        slider = DSlider()
        slider.delegate = self
        slider.minValue = 0.0
        slider.maxValue = 8.0
        slider.thumImg.image = UIImage(named: "slider_p")
        slider.setColors([HEX_RED.color(), HEX_RED.color()])
        
        addSubview(leftBtn)
        addSubview(rightBtn)
        addSubview(slider)
        leftBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().inset(k_w(20))
            make.top.equalToSuperview().offset(k_h(10))
            make.size.equalTo(CGSize(width: 20, height: 20))
        }
        
        rightBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(k_w(20))
            make.top.equalToSuperview().offset(k_h(10))
            make.size.equalTo(CGSize(width: 20, height: 20))
        }
        
        slider.snp.makeConstraints { make in
            make.height.equalTo(k_h(30))
            make.centerY.equalTo(leftBtn)
            make.left.equalTo(leftBtn.snp.right).offset(k_w(16))
            make.right.equalTo(rightBtn.snp.left).offset(k_w(-16))
        }
    }
    
    @objc func actionForBtn(sender:UIButton){
        if slider.currentValue <= slider.minValue && sender == leftBtn {return}
        if slider.currentValue >= slider.maxValue && sender == rightBtn {return}
        slider.currentValue = slider.currentValue.rounded!(1) + (sender == leftBtn ? -0.1 : 0.1)
        guard let model = model else{return}
        model.delay = Float(slider.currentValue)
        NotifyTool.post(updateDelay, model)
        block?(model)
    }
    
    var model : ChannelModel!{
        didSet{
            slider.currentValue = CGFloat(model.delay)
            
            leftBtn.isEnabled = model.enable
            rightBtn.isEnabled = model.enable
            slider.enable = model.enable
        }
    }
}

//MARK: - DSliderDelgate
extension DelayFooter : DSliderDelgate{
    func valueForChange(_ sender: DSlider) {
        guard let model = model else{return}
        model.delay = Float(sender.currentValue)
        NotifyTool.post(updateDelay, model)
    }
    func valueForChangeEnd(_ sender: DSlider) {
        guard let model = model else{return}
        model.delay = Float(sender.currentValue)
        block?(model)
    }
    func actionForInterval(_ sender: DSlider, _ time: Double) {
        guard let model = model else{return}
        block?(model)
    }
}

